from django.shortcuts import render,HttpResponse,redirect
from.models import *
from django.db.models import Q
# Create your views here.
def index(request):
    return HttpResponse('这是首页')


def adduser(request):
    if request.method == 'GET':
        user = User.objects.all()
        return render(request,'adduser.html',{'user':user})
    else:
        uname = request.POST.get('uname')
        truename = request.POST.get('truename')
        pwd = request.POST.get('pwd')
        tel = request.POST.get('tel')
        sex = request.POST.get('sex')
        roleid = request.POST.get('roleid')
        address = request.POST.get('address')
        User.objects.create(
        uname=uname,
        truename=truename,
        pwd=pwd,
        tel=tel,
        sex=sex,
        roleid=roleid,
        address=address
        )
        return HttpResponse('添加成功')


def show_user(request):
    user1 =User.objects.all()
    status =request.GET.get('status',-1)
    if status == '-1':
        user2 = User.objects.all()
    else:
        user2 = User.objects.filter(id=int(status)).all()

    return render(request,'show_user.html',{'user1':user1,'user2':user2})


def update_user(request):
    id = request.GET.get('id')
    status =request.GET.get('status')
    user =User.objects.get(id=int(id))
    user.status =int(status)
    user.save()
    return redirect('users:show_user')


def del_user(request):
    id = request.GET.get('id')
    user = User.objects.get(id=id)
    user.delete()
    return redirect('users:show_user')

def register(request):
    if request.method == 'GET':
        return render(request,'register.html')
    else:
        uname =request.POST.get('uname')
        pwd = request.POST.get('pwd')
        User.objects.create(
            uname=uname,
            pwd=pwd,
        )
        return redirect('users:show_user')